package jehc.cloud.gen.service.generator;
import jehc.cloud.common.util.StringUtil;
import jehc.cloud.gen.dao.XtScriptDao;
import jehc.cloud.gen.model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
/**
 * @Desc 生成页面层代码（包括JS，HTML代码）
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Slf4j
public class GeneratorPage extends GeneratorUtil {
	@Autowired
    XtScriptDao xtScriptDao;
	/**
	 * 创建附件回显（Bootstrap风格）
	 * @param xtGenerator
	 * @return
	 */
	public static String createBAttachmentObject(XtGenerator xtGenerator){
		List<XtGeneratorTableColumnForm> xtGeneratorTableColumnFormList = xtGenerator.getXt_Generator_Table_Column_FormList();
		boolean isExistFile=false;
		for(int i = 0; i < xtGeneratorTableColumnFormList.size(); i++){
			XtGeneratorTableColumnForm xtGeneratorTableColumnForm = xtGeneratorTableColumnFormList.get(i);
			if(!StringUtil.isEmpty(xtGeneratorTableColumnForm.getColumn_type()) && "5".equals(xtGeneratorTableColumnForm.getColumn_type())){
				isExistFile=true;
				break;
			}
		}
		if(isExistFile){
			StringBuffer sb = new StringBuffer();
			//回显附件使用
			sb.append("/**配置附件回显方法开始**/\r\n");
			sb.append("var params = {");
			String kv ="";
			String filed_name="";
			for(int i = 0; i < xtGeneratorTableColumnFormList.size(); i++){
				XtGeneratorTableColumnForm xtGeneratorTableColumnForm = xtGeneratorTableColumnFormList.get(i);
				if(null != xtGeneratorTableColumnForm.getColumn_type() && !"".equals(xtGeneratorTableColumnForm.getColumn_type()) && "5".equals(xtGeneratorTableColumnForm.getColumn_type())){
					if(null != kv && !"".equals(kv)){
						kv = kv+"+','+"+"$('#"+xtGeneratorTableColumnForm.getColumn_name()+"').val()";
						filed_name = filed_name+","+xtGeneratorTableColumnForm.getColumn_name();
					}else{
						kv = "$('#"+xtGeneratorTableColumnForm.getColumn_name()+"').val()";
						filed_name = xtGeneratorTableColumnForm.getColumn_name();
					}
				}
			}
			sb.append("xt_attachment_id:"+kv+","+"field_name:'"+filed_name+"'");
			sb.append("};\r\n");
			sb.append("ajaxBFilePathBackRequest('../xtCommonController/getAttachmentPathPP',params);\r\n");
			sb.append("/**配置附件回显方法结束**/\r\n");
			return sb.toString();
		}else{
			return "";
		}
	}

	/**
	 * 创建附件右键（Bootstrap风格）
	 * @param xtGenerator
	 * isUpAndDelete 1表示拥有上传和删除功能 即新增编辑页面使用2表示不拥有上传和删除功能 即明细页面使用
	 * @return
	 */
	public static String createAttachmentBRight(XtGenerator xtGenerator,int isUpAndDelete){
		StringBuffer sb = new StringBuffer();
		List<XtGeneratorTableColumnForm> xtGeneratorTableColumnFormList = xtGenerator.getXt_Generator_Table_Column_FormList();
		boolean isExistFile=false;
		for(int i = 0; i < xtGeneratorTableColumnFormList.size(); i++){
			XtGeneratorTableColumnForm xtGeneratorTableColumnForm = xtGeneratorTableColumnFormList.get(i);
			if(null != xtGeneratorTableColumnForm.getColumn_type() && !"".equals(xtGeneratorTableColumnForm.getColumn_type()) && "5".equals(xtGeneratorTableColumnForm.getColumn_type())){
				isExistFile=true;
				break;
			}
		}
		if(isExistFile){
			//回显附件使用
			if(isUpAndDelete == 2){
				sb.append("/**初始化附件右键菜单开始 参数4为1表示不拥有上传和删除功能 即明细页面使用**/\r\n");
			}else if(isUpAndDelete == 1){
				sb.append("/**初始化附件右键菜单开始 参数4为1表示拥有上传和删除功能 即新增和编辑页面使用**/\r\n");
			}
			for(int i = 0; i < xtGeneratorTableColumnFormList.size(); i++){
				XtGeneratorTableColumnForm xtGeneratorTableColumnForm = xtGeneratorTableColumnFormList.get(i);
				if(null != xtGeneratorTableColumnForm.getColumn_type() && !"".equals(xtGeneratorTableColumnForm.getColumn_type()) && "5".equals(xtGeneratorTableColumnForm.getColumn_type())){
					if(isUpAndDelete == 2){
						sb.append("initBFileRight('"+xtGeneratorTableColumnForm.getColumn_name()+"','"+xtGeneratorTableColumnForm.getColumn_name()+"_pic',2);\r\n");
					}else if(isUpAndDelete == 1){
						sb.append("initBFileRight('"+xtGeneratorTableColumnForm.getColumn_name()+"','"+xtGeneratorTableColumnForm.getColumn_name()+"_pic',1);\r\n");
					}
				}
			}
			sb.append("/**初始化附件右键菜单结束**/\r\n");
			return sb.toString();
		}else{
			return "";
		}
	}

	/////////////////////////////以下一对多操作//////////////////////////////
	/**
	 * 创建一对多附件右键（Bootstrap风格）
	 * @param xtGeneratorTableManyToOne
	 * isUpAndDelete 1表示拥有上传和删除功能 即新增编辑页面使用2表示不拥有上传和删除功能 即明细页面使用
	 * @return
	 */
	public static String createBAttachmentOneToManyRight(XtGeneratorTableManyToOne xtGeneratorTableManyToOne,int isUpAndDelete){
		StringBuffer sb = new StringBuffer();
		List<XtGeneratorTableColumnManyToOne> xtGeneratorTableColumnManyToOneList = xtGeneratorTableManyToOne.getXt_Generator_Table_ColumnMany_To_OneList();
		boolean isExistFile=false;
		for(int i = 0; i < xtGeneratorTableColumnManyToOneList.size(); i++){
			XtGeneratorTableColumnManyToOne xtGeneratorTableColumnManyToOne = xtGeneratorTableColumnManyToOneList.get(i);
			if(null != xtGeneratorTableColumnManyToOne.getColumn_type() && !"".equals(xtGeneratorTableColumnManyToOne.getColumn_type()) && "5".equals(xtGeneratorTableColumnManyToOne.getColumn_type())){
				isExistFile=true;
				break;
			}
		}
		if(isExistFile){
			//回显附件使用
			if(isUpAndDelete == 2){
				sb.append("\t/**初始化附件右键菜单开始 参数4为1表示不拥有上传和删除功能 即明细页面使用**/\r\n");
			}else if(isUpAndDelete == 1){
				sb.append("\t/**初始化附件右键菜单开始 参数4为1表示拥有上传和删除功能 即新增和编辑页面使用**/\r\n");
			}
			for(int i = 0; i < xtGeneratorTableColumnManyToOneList.size(); i++){
				XtGeneratorTableColumnManyToOne xtGeneratorTableColumnManyToOne = xtGeneratorTableColumnManyToOneList.get(i);
				if(null != xtGeneratorTableColumnManyToOne.getColumn_type() && !"".equals(xtGeneratorTableColumnManyToOne.getColumn_type()) && "5".equals(xtGeneratorTableColumnManyToOne.getColumn_type())){
//					if(isUpAndDelete == 2){
//						sb.append("\tinitBFileRight('"+lowfristchar(xt_Generator_TableMany_To_One.getXt_generator_one_to_many_table_name())+"_'+numbers+'_"+xt_Generator_Table_ColumnMany_To_One.getCOLUMN_NAME()+"','"+lowfristchar(xt_Generator_TableMany_To_One.getXt_generator_one_to_many_table_name())+"_'+numbers+'_"+xt_Generator_Table_ColumnMany_To_One.getCOLUMN_NAME()+"_pic',2);\r\n");
//					}else if(isUpAndDelete == 1){
//
//					}
					sb.append("\tinitBFileRight('"+lowfristchar(xtGeneratorTableManyToOne.getXt_generator_one_to_many_table_name())+"_'+numbers+'_"+xtGeneratorTableColumnManyToOne.getCOLUMN_NAME()+"','"+lowfristchar(xtGeneratorTableManyToOne.getXt_generator_one_to_many_table_name())+"_'+numbers+'_"+xtGeneratorTableColumnManyToOne.getCOLUMN_NAME()+"_pic',"+isUpAndDelete+");\r\n");
				}
			}
			sb.append("\t/**初始化附件右键菜单结束**/\r\n");
			return sb.toString();
		}else{
			return "";
		}
	}

	/**
	 *  创建附件回显（Bootstrap风格）
	 * @param xtGeneratorTableColumnManyToOnes
	 * @param xtGenerator
	 * @param isAddUpdateOrDetail 新增编辑 还是明细
	 * @return
	 */
	public static String createBAttachmentOneToManyObject(List<XtGeneratorTableColumnManyToOne> xtGeneratorTableColumnManyToOnes,XtGenerator xtGenerator,XtGeneratorTableManyToOne xtGeneratorTableManyToOne,int isAddUpdateOrDetail){
		StringBuffer sb = new StringBuffer();
		////////////////////////////////////////追加子表附件回显开始//////////////////////////////////
		boolean subExistFile = false;
		for(int j = 0;j < xtGeneratorTableColumnManyToOnes.size();j++){
			XtGeneratorTableColumnManyToOne xtGeneratorTableColumnManyToOne = xtGeneratorTableColumnManyToOnes.get(j);
			String dataType = xtGeneratorTableColumnManyToOne.getDATA_TYPE();
			String columne_type = xtGeneratorTableColumnManyToOne.getColumn_type();
			if("String".equals(sqlType2PageType(dataType))){
				if(("5").equals(columne_type)){//文件框
					subExistFile = true;
					break;
				}
			}
		}
		if(subExistFile){
			String lowfristTableName = lowfristchar(xtGeneratorTableManyToOne.getXt_generator_one_to_many_table_name());
			sb.append("//回调子表（"+xtGeneratorTableManyToOne.getXt_generator_one_to_many_table_name_zh()+"）附件回显操作开始\r\n");
			sb.append("var "+lowfristTableName+" = "+lowfristchar(xtGenerator.getXt_generator_tbname())+"Obj.items[0]."+lowfristTableName+";\r\n");
			sb.append("for(var i = 0; i < "+lowfristTableName+".length; i++){\r\n");
			for(int j = 0;j < xtGeneratorTableColumnManyToOnes.size();j++){
				XtGeneratorTableColumnManyToOne xtGeneratorTableColumnManyToOne = xtGeneratorTableColumnManyToOnes.get(j);
				String column_name= xtGeneratorTableColumnManyToOne.getCOLUMN_NAME();
				String dataType = xtGeneratorTableColumnManyToOne.getDATA_TYPE();
				String columne_type = xtGeneratorTableColumnManyToOne.getColumn_type();
				if("String".equals(sqlType2PageType(dataType))){
				if(("5").equals(columne_type)){//文件框
					//追加子表附件右键初始化
					sb.append("\tinitBFileRight('"+lowfristchar(xtGeneratorTableManyToOne.getXt_generator_one_to_many_table_name())+"_'+i+'_"+xtGeneratorTableColumnManyToOne.getCOLUMN_NAME()+"','"+lowfristchar(xtGeneratorTableManyToOne.getXt_generator_one_to_many_table_name())+"_'+i+'_"+xtGeneratorTableColumnManyToOne.getCOLUMN_NAME()+"_pic',"+isAddUpdateOrDetail+");\r\n");
					//回显
					sb.append("\tvar params = {xt_attachment_id:$('#"+lowfristTableName+"_'+i+'_"+column_name+"').val(),field_name:'"+lowfristTableName+"_'+i+'_"+column_name+"'};\r\n");
					sb.append("\tajaxBFilePathBackRequest(rest_url_prefix_xt+'/xtCommon/attAchmentPathpp',params);\r\n");

					}
				}
			}
			sb.append("}\r\n");
			sb.append("//回调子表（"+xtGeneratorTableManyToOne.getXt_generator_one_to_many_table_name_zh()+"）附件回显操作结束\r\n");
		}
		////////////////////////////////////////追加子表附件回显结束//////////////////////////////////
		return sb.toString();
	}
}
